<template>
  <BasicTree
    v-if="treeData.length"
    title="菜单列表"
    toolbar
    search
    treeWrapperClassName="h-[calc(100%-35px)] overflow-auto"
    :clickRowToExpand="false"
    :treeData="treeData"
    :fieldNames="{ key: 'id', title: 'menuTitle' }"
    :defaultExpandAll="true"
    @select="handleSelect"
  />
</template>
<script lang="ts">
  import { defineComponent, onMounted, onUpdated, ref, watch } from 'vue';
  import { BasicTree, TreeItem } from '/@/components/Tree';
  import { queryCheckedMenuTreeListByPermissionId } from '/@/api/system/system';
  import { propTypes } from '/@/utils/propTypes';
  import { PermissionMenuReq } from '/@/api/system/modal/permissionMenuModel';

  export default defineComponent({
    name: 'MenuTree',
    components: { BasicTree },
    props: {
      permissionId: propTypes.string,
    },
    emits: ['select'],
    setup(props, { emit }) {
      const treeData = ref<TreeItem[]>([]);

      async function loadData() {
        const permissionMenuReq: PermissionMenuReq = {
          permissionId: props.permissionId,
        };
        treeData.value = (await queryCheckedMenuTreeListByPermissionId(
          permissionMenuReq,
        )) as unknown as TreeItem[];
      }

      function handleSelect(keys) {
        emit('select', keys[0]);
      }

      watch(
        () => props.permissionId,
        () => {
          treeData.value = [];
          loadData();
        },
        { deep: true },
      );

      onUpdated(() => {});
      onMounted(() => {
        loadData();
      });
      return { treeData, handleSelect };
    },
  });
</script>
